package com.epalmpay.service.app.impl;

import com.alibaba.fastjson.JSONObject;
import com.epalmpay.entity.Shop;
import com.epalmpay.entity.Userbase;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.mapper.ShopMapper;
import com.epalmpay.mapper.UserbaseMapper;
import com.epalmpay.service.app.IAppUserService;
import com.epalmpay.status.Rescode;
import com.epalmpay.util.Const;
import com.epalmpay.util.HttpClientUtil;
import com.epalmpay.util.Response;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/6.
 */
@Service("appUserService")
public class IAppUserServiceImpl implements IAppUserService {

    @Resource
    private ShopMapper shopMapper;

    @Resource
    private UserbaseMapper userbaseMapper;

    @Override
    public Response updateShopBusiness(String shopId, String shopName, String shopPhone, String wordTime, String icon, String provinceCityDistrict, String address, String memo) throws ApplicationException {
        Map<String, Object> map = new HashMap<String, Object>();
        //根据地址查询经纬度
        String detailed=provinceCityDistrict+address;
        String result= HttpClientUtil.get(Const.GEOCODER+"?="+detailed+"&key="+Const.MAP_KEY);
        JSONObject jsonObj=JSONObject.parseObject(result);
        Object statusObject=jsonObj.get("status");
        Integer status=Integer.parseInt(String.valueOf(statusObject));
        if(!status.equals(0)){
            throw new ApplicationException(Rescode.FAIL,"地理位置解析异常,请检查区域管理地址的填写");
        }
        JSONObject resultObject=jsonObj.getJSONObject("result");
        JSONObject address_componentsObject=resultObject.getJSONObject("address_components");//地址详细信息
        JSONObject locationObject=resultObject.getJSONObject("location");//经纬度对象
        String province=address_componentsObject.getString("province");
        String city=address_componentsObject.getString("city");
        String district=address_componentsObject.getString("district");
        Double lng=locationObject.getDouble("lng");
        Double lat=locationObject.getDouble("lat");

        Shop shop = new Shop();
        shop.setId(Long.parseLong(shopId));
        shop.setMerchantName(shopName);
        shop.setPhone(shopPhone);
        shop.setWorkTime(wordTime);
        shop.setIcon(icon);
        shop.setAddressProvince(province);// 省
        shop.setAddressCity(city);// 市
        shop.setAddressDistrict(district);// 区
        shop.setAddress(address);
        shop.setLatitude(lat.toString());
        shop.setLongitude(lng.toString());
        shop.setMemo(memo);
        shopMapper.updateByPrimaryKeySelective(shop);

        return Response.getSuccess(map);
    }

    @Override
    public Response updateShopOperating(String shopId, String businessLicense, String licensePicurl, String principal, String principalPhone) throws ApplicationException {
        Map<String, Object> map = new HashMap<String, Object>();
        Shop shop = new Shop();
        shop.setId(Long.parseLong(shopId));
        shop.setBusinessLicense(businessLicense);
        shop.setPrincipalName(principal);
        shop.setPrincipalMobile(principalPhone);
        shop.setQrImg(licensePicurl);
        shopMapper.updateByPrimaryKeySelective(shop);
        return Response.getSuccess(map);
    }

    @Override
    public Response updatepassword(String userId, String oldPassword, String newPassword) throws ApplicationException {
        Map<String, Object> map = new HashMap<String, Object>();
        Userbase userbase = userbaseMapper.selectByPrimaryKey(Long.parseLong(userId));
        if (userbase == null) {
            throw new ApplicationException(Rescode.FAIL, "用户信息不存在");
        }
        String passwd = new SimpleHash("SHA-1", userbase.getUsername(), oldPassword).toString();
        if (passwd.equals(userbase.getPassword())) {
            userbase.setId(Long.parseLong(userId));
            userbase.setPassword(new SimpleHash("SHA-1", userbase.getUsername(), newPassword).toString());
            userbase.setGmtModified(new Date());
            userbaseMapper.updateByPrimaryKeySelective(userbase);
        } else {
            throw new ApplicationException(Rescode.FAIL, " 原始密码输入不对,不能去修改");
        }
        return Response.getSuccess(map);
    }

    @Override
    public Response resetpassword(String username, String password,String phone) throws ApplicationException {
        Map<String, Object> map = new HashMap<String, Object>();
        Userbase userbase = userbaseMapper.queryUserByName(username);

        Shop shop = shopMapper.selectShopPhoneByUserId(userbase.getId());
        if(shop !=null){
            if(!shop.getPrincipalMobile().equals(phone)){
                throw new ApplicationException(Rescode.FAIL, "非负责人手机号不能重置密码");
            }

        }if (userbase == null) {
            throw new ApplicationException(Rescode.FAIL, "门店信息不存在");
        }

        if (userbase != null) {
            userbase.setPassword(new SimpleHash("SHA-1", userbase.getUsername(), password).toString());
            userbase.setGmtModified(new Date());
            userbaseMapper.updateByPrimaryKeySelective(userbase);
        } else {
            throw new ApplicationException(Rescode.FAIL, "用户信息不存在");
        }
        return Response.getSuccess(map);
    }
}
